package com.wjy.rbac.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjy.rbac.entity.Power;
import com.wjy.rbac.entity.PowerRequestMethod;
import com.wjy.rbac.entity.PowerRequestUrl;
import com.wjy.rbac.mapper.PowerMapper;
import com.wjy.rbac.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ybxxszl
 * @since 2019-04-12
 */
@Service
public class PowerServiceImpl extends ServiceImpl<PowerMapper, Power> implements IPowerService {

    @Autowired
    @Qualifier(value = "powerRequestMethodServiceImpl")
    private IPowerRequestMethodService powerRequestMethodService;

    @Autowired
    @Qualifier(value = "powerRequestUrlServiceImpl")
    private IPowerRequestUrlService powerRequestUrlService;

    @Autowired
    @Qualifier(value = "requestMethodServiceImpl")
    private IRequestMethodService requestMethodService;

    @Autowired
    @Qualifier(value = "requestUrlServiceImpl")
    private IRequestUrlService requestUrlService;

    @Override
    public void insert(Power power) {
        power.setStatus(0);
        save(power);
    }

    @Override
    public void update(Power power) {
        power.setStatus(0);
        updateById(power);
    }

    @Override
    public void delete(String powerId) {
        removeById(powerId);
    }

    @Override
    public Map<String, Object> get(String powerId) {
        Map<String, Object> result = new HashMap<String, Object>();
        Power power = getById(powerId);
        result.put("power", power);
        List<PowerRequestMethod> powerRequestMethodList = powerRequestMethodService.getByPowerId(powerId);
        List<String> requestMethodIdList = new ArrayList<String>();
        for (PowerRequestMethod powerRequestMethod : powerRequestMethodList) {
            requestMethodIdList.add(powerRequestMethod.getRequestMethodId());
        }
        List<Map<String, Object>> requestMethodList = requestMethodService.getByRequestMethodIdList(requestMethodIdList);
        result.put("requestMethodList", requestMethodList);
        List<PowerRequestUrl> powerRequestUrlList = powerRequestUrlService.getByPowerId(powerId);
        List<String> requestUrlIdList = new ArrayList<String>();
        for (PowerRequestUrl powerRequestUrl : powerRequestUrlList) {
            requestUrlIdList.add(powerRequestUrl.getRequestUrlId());
        }
        List<Map<String, Object>> requestUrlList = requestUrlService.getByRequestUrlIdList(requestUrlIdList);
        result.put("requestUrlList", requestUrlList);
        return result;
    }

    @Override
    public List<Map<String, Object>> getByPowerIdList(List<String> powerIdList) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (String powerId : powerIdList) {
            result.add(get(powerId));
        }
        return result;
    }

}
